$(function () {

	function _operation_field_formatter(cellValue , options , rowData){
		var mode = 'view';
		var buttons = [];
		var id = rowData['userId'];
		buttons.push('<div class="grid-operation-button">')

		if(mode == 'view') {
			buttons.push(str_format('<a class="label label-info" href="../live/live_detail.html?uid={0}">查看</a>',rowData['userId']));
		}
		if(mode == 'modify') {
			buttons.push(str_format('<a class="label label-info" onclick="T.vm.update(' + id + ',event)" href="javascript:;">修改</a>', rowData['userId']));
		}
		buttons.push('</div>');
		return buttons.join("&nbsp;")
	}

    $("#jqGrid").jqGrid({
        url: baseURL + 'dataview/viewuserfullinfo/getLiverInfolist',
		styleUI: 'Bootstrap',
		caption: "主播列表",
        datatype: "json",
        colModel: [			
			{ label: 'userId', name: 'userId', index: 'user_id', width: 50, key: true },
			{ label: '头像', name: 'avanta', width: 80 ,formatter : set_images_formatter},
			{ label: '用户昵称', name: 'nickname', index: 'nickname', width: 80 ,formatter : get_grid_column_formatter('dataview/viewuserfullinfo/nickname')},
			//{ label: '用户名', name: 'username', index: 'username', width: 80 ,formatter : get_grid_column_formatter('dataview/viewuserfullinfo/username')},
			{ label: '手机号', name: 'mobile', index: 'mobile', width: 80 ,formatter : get_grid_column_formatter('dataview/viewuserfullinfo/mobile')},
			{ label: '剩余友币', name: 'coin', index: 'coin', width: 80 ,formatter : get_grid_column_formatter('dataview/viewuserfullinfo/username')},
			{ label: '可用余额', name: 'balance', index: 'balance', width: 80 ,formatter : get_grid_column_formatter('dataview/viewuserfullinfo/username')},
			{ label: '累计消费友币', name: 'totalAmount', index: 'totalAmount', width: 80 ,formatter : get_grid_column_formatter('dataview/viewuserfullinfo/username')},
			{ label: '累计收益友豆', name: 'totalBeans', index: 'totalBeans', width: 80 ,formatter : get_grid_column_formatter('dataview/viewuserfullinfo/username')},
			{ label: '累计收益金额', name: 'totalIncome', index: 'totalIncome', width: 80 ,formatter : get_grid_column_formatter('dataview/viewuserfullinfo/username')},
			{ label: '累计提现金额', name: 'totalWithdraw', index: 'totalWithdraw', width: 80 ,formatter : get_grid_column_formatter('dataview/viewuserfullinfo/mobile')},
			{ label: '主播等级', name: 'level', index: 'level', width: 80 ,formatter : get_grid_column_formatter('dataview/viewuserfullinfo/username')},
			{ label: '所属公会', name: 'guildId', index: 'guildId', width: 80 ,formatter : get_grid_column_formatter('dataview/viewuserfullinfo/username')},
			{ label: '是否实名', name: 'isVerifyIdcard', index: 'isVerifyIdcard', width: 80 ,formatter : get_grid_column_formatter('dataview/viewuserfullinfo/isVerifyIdcard')},
			{ label: '操作', name: '_operation_field', index: '_operation_field', width:80 ,formatter:_operation_field_formatter}
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm  = T.vm = new Vue({
	el:'#webpage',
	data:{
		appearance : {
			showList: true,
			showControllBar : true,
			showSearchForm : true
		},
		centerDialogVisible: false,
		q:{
			nickname:'',
			user_id:'',
			guild_id:'',
			is_verify_idcard:''
		},
		options: [{
			value: '',
			label: '全部'
		}, {
			value: '0',
			label: '未认证'
		}, {
			value: '1',
			label: '已认证'
		}],
		title: null,
		cooperations:[{
			value: 1,
			label: '一年'
		}, {
			value: 2,
			label: '二年'
		}, {
			value: 3,
			label: '三年'
		}],
		sharerates:[{
			value: 0.3,
			label: '30%'
		},{
			value: 0.35,
			label: '35%'
		}, {
			value: 0.4,
			label: '40%'
		}, {
			value: 0.45,
			label: '45%'
		},{
			value: 0.5,
			label: '50%'
		}],
		viewUserFullInfo: {
			user_id: '',
			realname: '',
			sharerate: 0.4,
			cooperation: 1,
			desc: '',
		},
		rules: {
			user_id: [
				{ required: true, message: '用户id', trigger: 'blur' },
			],
			realname: [
				{ required: true, message: '真实姓名', trigger: 'change' }
			],
			sharerate: [
				{ required: true, message: '分成比例', trigger: 'change' }
			],
			cooperation: [
				{ required: true, message: '合约期限', trigger: 'change' }
			],
			desc: [
				{ required: true, message: '备注', trigger: 'blur' },
				{ min: 10, max: 40, message: '长度在 10 到 40 个字符', trigger: 'blur' }
			]
		}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.centerDialogVisible = true;
			vm.viewUserFullInfo = {};
		},
		update: function (targetId,event) {
			var userId = targetId || getSelectedRow();
			if(userId == null){
				return ;
			}
			vm.appearance.showList = false;
            vm.title = "修改";
            
            vm.getInfo(userId)
		},
		saveOrUpdate: function (event) {
			var url = vm.viewUserFullInfo.userId == null ? "dataview/viewuserfullinfo/save" : "dataview/viewuserfullinfo/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.viewUserFullInfo),
			    success: function(r){
			    	if(r.code > 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var userIds = getSelectedRows();
			if(userIds == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "dataview/viewuserfullinfo/delete",
                    contentType: "application/json",
				    data: JSON.stringify(userIds),
				    success: function(r){
						if(r.code > 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(userId){
			$.get(baseURL + "dataview/viewuserfullinfo/info/"+userId, function(r){
                vm.viewUserFullInfo = r.viewUserFullInfo;
            });
		},
		reload: function (event) {
			vm.appearance.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
				postData:vm.q,
                page:page
            }).trigger("reloadGrid");
		}
	}
});